package one.stand.controller;

import com.jmp.api.crm.dto.CompanyShowroomResponse;
import com.jmp.api.crm.dto.CompanyShowroomSaveRequest;
import com.jmp.autocofiguration.web.interceptor.SecurityAuthCheck;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import one.stand.model.CompanyShowroom;
import one.stand.model.ResultModel;
import one.stand.service.CompanyShowroomService;
import one.stand.util.RequestContextHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

@Api(description = "优选审核")
@RestController
@RequestMapping("crmPc/showroom")
@Slf4j
public class CompanyShowroomController {
    @Autowired
    private CompanyShowroomService companyShowroomService;

    @ApiOperation("详情")
    @PostMapping("")
    @SecurityAuthCheck(company = true, card = true)
    public ResultModel<CompanyShowroomResponse> info() {
        final Integer userCompanyId = RequestContextHolder.getRequestContext().getUser().getCompanyId();

        CompanyShowroom companyShowroom = companyShowroomService.getByCompanyId(userCompanyId);

        if (Objects.nonNull(companyShowroom)) {
            CompanyShowroomResponse response = CompanyShowroomResponse.builder()
                    .showroomId(companyShowroom.getId())
                    .background(companyShowroom.getBackground())
                    .showVideo(companyShowroom.getShowVideo())
                    .vrUrl(companyShowroom.getVrUrl())
                    .videoUrl(companyShowroom.getVideo())
                    .showCode(companyShowroom.getShowCode())
                    .build();
            return ResultModel.success(response);
        }

        return ResultModel.success();
    }

    @ApiOperation("保存")
    @PostMapping("save")
    @SecurityAuthCheck(company = true, card = true)
    public ResultModel save(@RequestAttribute CompanyShowroomSaveRequest request) {

        CompanyShowroom companyShowroom = new CompanyShowroom();
        companyShowroom.setId(request.getShowroomId());
        companyShowroom.setCompanyId(request.getCId());
        companyShowroom.setBackground(request.getBackground());
        companyShowroom.setShowVideo(request.getShowVideo());
        companyShowroom.setVideo(request.getVideoUrl());
        companyShowroom.setVrUrl(request.getVrUrl());
        companyShowroom.setShowCode(request.getShowCode());
        companyShowroomService.save(companyShowroom);

        return ResultModel.success();
    }
}
